package com.eagle.security.entries;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collection;

import org.springframework.security.access.expression.SecurityExpressionRoot;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.util.Assert;

/**
 * 用户
 * 
 * @author Xu Jin
 * 
 */
public class User extends org.springframework.security.core.userdetails.User
{
    // 缺省空权限
    private static final SecurityExpressionRoot DEFAULT_SECURITY = new SecurityExpressionRoot(new UsernamePasswordAuthenticationToken(null, null, null)) {
    };

    // 用户id
    private String id;

    // 用户昵称
    private String nickname;

    // IP
    private String ip;

    // 创建时间
    private Timestamp createDate = new Timestamp(System.currentTimeMillis());

    // 客户端
    private String clientInfo;

    private static final long serialVersionUID = 1L;

    // 权限判断对象
    private SecurityExpressionRoot security = DEFAULT_SECURITY;

    // 用户额外信息
    private UserExtendInfo extendInfo = new UserExtendInfo();

    // 账号开始使用时间，默认为空不做限制
    private Timestamp startUseTime;

    // 账号结束使用时间，默认为空不做限制
    private Timestamp endUseTime;

    // 过期类型
    private int overdueType = 1 ;//OverdueTypeConstant.DELAY;

    // 备注
    private String remark;

    /**
     * 构造函数
     * 
     * @param username
     *            用户名
     * @param password
     *            密码
     * @param nickname
     *            昵称
     * @param enabled
     *            用户是否有效
     */
    public User(String username, String password, String nickname, boolean enabled)
    {
        super(username, password, enabled, true, true, true, new ArrayList<GrantedAuthority>());
        this.nickname = nickname;
    }

    /**
     * @see org.springframework.security.core.userdetails.User.User(String, String, boolean,
     *      boolean, boolean, boolean, Collection<? extends GrantedAuthority>)
     */
    public User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked,
            Collection<? extends GrantedAuthority> authorities)
    {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
    }

    /**
     * @see org.springframework.security.core.userdetails.User.User(String, String, Collection<?
     *      extends GrantedAuthority>)
     */
    public User(String username, String password, Collection<? extends GrantedAuthority> authorities)
    {
        super(username, password, authorities);
    }

    public User(String username, String password, Collection<? extends GrantedAuthority> authorities, String id, String nickname)
    {
        super(username, password, authorities);
        this.id = id;
        this.nickname = nickname;
    }

    public User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked,
            Collection<? extends GrantedAuthority> authorities, String id, String nickname)
    {
        super(username, password, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, authorities);
        this.id = id;
        this.nickname = nickname;
    }

    /**
     * 获取当前用户IP地址
     * 
     * @return String
     */
    public String getIp()
    {
        return ip;
    }

    /**
     * 设置IP地址
     * 
     * @param ip
     */
    public void setIp(String ip)
    {
        this.ip = ip;
    }

    /**
     * 获取创建时间
     * 
     * @return Date
     */
    public Timestamp getCreateDate()
    {
        return createDate;
    }

    /**
     * 设置创建时间
     * 
     * @param createDate
     *            Date
     */
    public void setCreateDate(Timestamp createDate)
    {
        this.createDate = createDate;
    }

    /**
     * 获取客户端信息
     * 
     * @return
     */
    public String getClientInfo()
    {
        return clientInfo;
    }

    /**
     * 设置客户端信息
     * 
     * @param clientInfo
     *            客户端信息
     */
    public void setClientInfo(String clientInfo)
    {
        this.clientInfo = clientInfo;
    }

    /**
     * 获取用户 ID
     * 
     * @return
     */
    public String getId()
    {
        return id;
    }

    /**
     * 获取用户昵称
     * 
     * @return
     */
    public String getNickname()
    {
        return nickname;
    }

    /**
     * 设置用户昵称
     * 
     * @param nickname
     *            用户昵称
     */
    public void setNickname(String nickname)
    {
        this.nickname = nickname;
    }

    /**
     * 返回账号开始使用时间
     * 
     * @return Timestamp
     */
    public Timestamp getStartUseTime()
    {
        return startUseTime;
    }

    /**
     * 设置账号开始使用时间
     * 
     * @param startUseTime
     *            Timestamp
     */
    public void setStartUseTime(Timestamp startUseTime)
    {
        this.startUseTime = startUseTime;
    }

    /**
     * 返回账号结束使用时间
     * 
     * @return Timestamp
     */
    public Timestamp getEndUseTime()
    {
        return endUseTime;
    }

    /**
     * 设置账号结束使用时间
     * 
     * @param endUseTime
     *            Timestamp
     */
    public void setEndUseTime(Timestamp endUseTime)
    {
        this.endUseTime = endUseTime;
    }

    /**
     * 获取用户备注
     * 
     * @return
     */
    public String getRemark()
    {
        return remark;
    }

    /**
     * 设置用户备注
     * 
     * @param remark
     */
    public void setRemark(String remark)
    {
        this.remark = remark;
    }

    /**
     * 获取过期类型
     * 
     * @return
     */
    public int getOverdueType()
    {
        return overdueType;
    }

    /**
     * 设置过期类型
     * 
     * @param overdueType
     */
    public void setOverdueType(int overdueType)
    {
        this.overdueType = overdueType;
    }

    /**
     * 获取用户额外信息
     * 
     * @return Map<Object, Object>
     */
    public UserExtendInfo getExtendInfo()
    {
        return extendInfo;
    }

    /**
     * 设置用户额外信息
     * 
     * @param extendInfo
     *            Map<Object, Object>
     */
    public void setExtendInfo(UserExtendInfo extendInfo)
    {
        Assert.notNull(extendInfo);
        this.extendInfo = extendInfo;
    }

    /**
     * 获取用户权限判断对象
     * 
     * @return SecurityExpressionRoot
     */
    public SecurityExpressionRoot getSecurity()
    {
        return security;
    }

    public void setSecurity(SecurityExpressionRoot security)
    {
        Assert.notNull(security);
        if (this.security == null || this.security == DEFAULT_SECURITY)
        {
            this.security = security;
        }
    }

    /**
     * 是否拥有指定角色
     * 
     * @param role
     *            角色名称
     * @return true 拥有；false 不拥有
     */
    public boolean hasRole(String role)
    {
        return security.hasRole(role);
    }

    /**
     * 是否拥有的指定的一个或多个角色
     * 
     * @param roles
     *            一个或多个角色名称
     * @return true 拥有；false 不拥有
     */
    public boolean hasRoles(String... roles)
    {
        for (String role : roles)
        {
            if (!security.hasRole(role))
                return false;
        }

        return true;
    }

    /**
     * 是否至少拥有指定的一个或多个角色中的一个
     * 
     * @param roles
     *            一个或多个角色名称
     * @return true 拥有；false 不拥有
     */
    public final boolean hasAnyRole(String... roles)
    {
        return security.hasAnyRole(roles);
    }
}